<template>
  <div class='goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <!-- <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">手机</XtxBreadItem>
        <XtxBreadItem to="/">华为</XtxBreadItem>
        <XtxBreadItem to="/">p30</XtxBreadItem>
      </XtxBread> -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item
        v-if="goods&&goods.categories" :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</el-breadcrumb-item
        >
        <el-breadcrumb-item v-if="goods&&goods.categories" :to="`/category/sub/${goods.categories[0].id}`">{{goods.categories[0].name}}</el-breadcrumb-item>
        <transition name="fade-right" mode="out-in">
          <el-breadcrumb-item v-if="goods">{{goods.name}}</el-breadcrumb-item>
        </transition>
      </el-breadcrumb>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <!-- 名字组件 -->
          <GoodsName :goods="goods"></GoodsName>
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku"></GoodsSku>
          <!-- 选择器 -->
          <div class="c"><span class="num">数量</span><el-input-number size="small" v-model="num" :min="1" :max="goods.inventory" @change="handleChange" /></div>
          <el-button type="primary" class="btn" @click="inserCart">加入购物车</el-button>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId = "goods.id" v-if="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTables></GoodsTables>
          <!-- 注意事项 -->
          <!-- <div class="goods-warn"></div> -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <!-- <div class="goods-aside"></div> -->
        <div class="goods-aside">
          <GoodsHot :type="1" :id="goods.id"></GoodsHot>
          <GoodsHot :type="2" :id="goods.id"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTables from './components/goods-tables.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { findGoods } from '../../api/product'
import { nextTick, ref, watch, provide } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
export default {
  name: 'GoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTables, GoodsHot, GoodsWarn },
  setup () {
    // 获取商品详情
    const goods = useGoods()
    // 判断是否选择规格组件
    const changeSku = (sku) => {
      // console.log(sku.skuId)
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      // 记录选择后的sku，可能有数据也可能为{}
      currSku.value = sku
    }
    // 提供goods属性给子孙组件用
    provide('goods', goods)
    // 按钮+ - 的数量
    const num = ref(1)
    const handleChange = (value) => {
      num.value = value
    }
    // console.log(goods)
    // 加入购物车
    const store = useStore()
    const currSku = ref(null)
    const inserCart = () => {
      // sku选择完整
      if (currSku.value && currSku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currSku.value
        const { id, name, price: nowPrice, mainPictures } = goods.value
        // console.log(goods.value)
        store.dispatch('cart/insertCart', {
          skuId,
          attrsText,
          stock,
          id,
          name,
          nowPrice,
          picture: mainPictures[0],
          isEffective: true,
          selected: true,
          count: num.value
        }).then(() => {
          ElMessage({
            type: 'success',
            message: '加入购车成功'
          })
        })
      } else {
        ElMessage({
          type: 'warning',
          message: '请选择完整规格'
        })
      }
    }
    return { goods, changeSku, num, handleChange, inserCart }
  }
}
const useGoods = () => {
  // 出现路由地址商品 Id发生变化但不会重新初始化组件
  const goods = ref(null)
  const route = useRoute()
  watch(() => route.params.id, (newValue) => {
    // 发送请求获取goods详情页
    if (newValue && route.path === `/product/${newValue}`) {
      findGoods(route.params.id).then(data => {
        // console.log(data)
        // 先把goods为空  组件就被销毁了
        goods.value = null
        // 等到下一次更新时 在赋值
        nextTick(() => {
          goods.value = data.result
        })
      })
    }
  }, { immediate: true })
  return goods
}
</script>

<style scoped lang='less'>
.fade-right-enter-from{
  transform: translateX(20px);
  opacity: 0;
}
.fade-right-enter-active{
  transition: all 1s ease;
}
.fade-right-enter-to{
  transform: translateX(0);
  opacity: 1;
}
.fade-right-leave-from{
  transform: translateX(0);
  opacity: 1;
}
.fade-right-leave-to{
  transform: translateX(-20px);
  opacity: 0;
}
.breadcrumb{
  padding-bottom: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.num{
  width: 50px;
  color: #999;
  margin-right: 20px;
}
.c{
  margin-left: 10px;
  margin-bottom: 20px;
}
.btn{
  margin-left: 70px;
}
</style>
